/**
 * 
 */

var electron = require('electron');
var ipcRenderer = electron.ipcRenderer;

/*
var callback;
ipcRenderer.on('cmd-reply', (event, arg) => {
    if(typeof callback){
        try {
            var data = JSON.parse(arg);
            callback(data);
        } catch (error) {
            callback({});
        }
        
    }
})*/


H5x.plugin("ipc",function(component){
    var count = 0;
    component.ipc = function(cmd,data,cb){
        count++;
        data.replycmd = "reply-"+count;

        //console.log(data);
        ipcRenderer.on(data.replycmd,function(event,arg){
            var rs = JSON.parse(arg);
            if(rs.end){
                ipcRenderer.removeAllListeners(data.replycmd);
            }
            if(typeof cb == "function"){
                cb(rs);
            }
        });

        ipcRenderer.send(cmd, JSON.stringify(data));        
    }
});